package com.joysuch.wwyt.bp.controller;


import com.joysuch.wwyt.bp.bean.change.BpChangeRecordApproveBean;
import com.joysuch.wwyt.bp.bean.change.BpChangeRecordCreateBean;
import com.joysuch.wwyt.bp.bean.change.BpChangeRecordListBean;
import com.joysuch.wwyt.bp.bean.change.BpChangeRecordQueryBean;
import com.joysuch.wwyt.bp.entity.change.BpChangeRecord;
import com.joysuch.wwyt.bp.service.BpChangeRecordService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/bp/change")
@Api(tags = "/bp/change 变更管理")
@CrossOrigin
public class BpChangeRecordController {

    @Autowired
    private BpChangeRecordService bpChangeRecordService;

    @Value("${wwyt.bp.open.change}")
    private boolean bpOpenChange;

    @PostMapping("/page")
    @ApiOperation(value = "查询列表(分页）" , response = BpChangeRecordListBean.class)
    public ResultBean page(@RequestBody BpChangeRecordQueryBean queryBean) {
       Pageable pageable = QPageRequest.of(queryBean.getPage(), queryBean.getSize());
       pageable= PageDataUtils.addOrderByDesc(pageable,"createTime");
       Page<BpChangeRecordListBean> page = bpChangeRecordService.pageList(queryBean, pageable);
       return  ResultBean.pageData(page.getContent(), page.getTotalElements());
    }

    @GetMapping("/getById")
    @ApiOperation(value = "获取详情(根据Id）" , response = BpChangeRecordListBean.class)
    public ResultBean getById(@RequestParam Long id) {
        BpChangeRecordListBean bean = bpChangeRecordService.findById(id);
        return ResultBean.success(bean);
    }

    @GetMapping("/deleteById")
    @RequiresPermissions("bp:change:delete")
    @ApiOperation(value = "删除(根据Id）")
    public ResultBean deleteById(@RequestParam Long id) {
        bpChangeRecordService.delete(id);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/applyChange")
    @ApiOperation(value = "申请变更" , response = BpChangeRecord.class)
    public ResultBean applyChange(@RequestBody BpChangeRecordCreateBean bean) {
          bpChangeRecordService.create(bean);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/approve")
    @RequiresPermissions("bp:change:approve")
    @ApiOperation(value = "审批" )
    public ResultBean approve(@RequestBody BpChangeRecordApproveBean bean) {
        try {
            bpChangeRecordService.approve(bean);
        } catch (Exception e) {
             return ResultBean.wrapErrorResultDefault("变更执行出错："+ e.getMessage());
        }
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/checkPendingChange")
//    @RequiresPermissions("bp:change:checkPending")
    @ApiOperation(value = "查看是否存在待审批的变更(根据Id, 变更类型）: true=存在， false=不存在" , response = Boolean.class)
    public ResultBean getById(@RequestParam Long id, @RequestParam String changeType) {
        boolean pendingChange = bpChangeRecordService.checkPendingChange(id, changeType);
        return ResultBean.success(pendingChange);
    }

    @GetMapping("/openChange")
    @ApiOperation(value = "变更申请开关: true=打开， false=关闭" , response = Boolean.class)
    public ResultBean openChange() {
        log.info("${wwyt.bp.open.change}: 变更申请开关： "+ bpOpenChange);
        return ResultBean.success(bpOpenChange);
    }


}
